import pickle
import cv2
from socket import *

# 创建一个等待链接的socket
S = socket()
# 给S绑定一个ip地址和端口号
S.bind((gethostname(),8890))
# S连接网络开启监听
S.listen()
# S接到连接消息，转接给s，并且记录对方的ip
s,addr = S.accept()
# 给连接者发送消息
print(addr)
# 设置捕获电脑区域
monitor = (0, 0, 2560, 1600)
s.send(pickle.dumps(monitor))

# 视频保存参数
fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps = 25.0
output_size = (1920, 1080)
output_file = 'screen_capture.avi'
# 创建 VideoWriter 对象
out = cv2.VideoWriter(output_file, fourcc, fps, output_size)
# 创建 windows 窗体
cv2.namedWindow("实时屏幕捕获".encode("gbk").decode('UTF-8', errors='ignore'), cv2.WINDOW_NORMAL)

while True:
    a = pickle.loads(s.recv())
    screenshot_np = pickle.loads(s.recv())
    # print(a)
    print(screenshot_np)
    # # 将帧写入视频流
    # out.write(screenshot_np)
    # # 显示屏幕截取画面
    # cv2.imshow("实时屏幕捕获".encode("gbk").decode('UTF-8', errors='ignore'), screenshot_np)
    # # 监控按键，按下 q 退出程序
    # if cv2.waitKey(1) & 0xFF == ord("q"):
    #     break
    # # 关闭 OpenCV 所有窗体
    # cv2.destroyAllWindows()